package com.gulu.wxworkrobot.Service;

import cn.hutool.http.HttpUtil;
import cn.hutool.json.JSONUtil;
import com.gulu.wxworkrobot.DTO.ContentDTO;
import com.gulu.wxworkrobot.DTO.MessageDTO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

/**
 * @ClassName: CgRobot
 * @Description: TODO
 * @Author: A80840
 * @Date: 2025/2/27 10:10
 * @Version 1.0
 **/
@Component
@Slf4j
public class CgRobot {
    private static final String ROBOT_URL = "https://qyapi.weixin.qq.com/cgi-bin/webhook/send?key=c916dace-706f-4bae-87b5-45626a9aa9f0";
    private static final String WEATHER_URL = "https://api.qweather.com/v7/weather/now?location=101280701&key=d49a4b46602e4bd98635377a99e458e6";

    private void getWeatherMsg() {
        String responseBody = null;
        try {
            responseBody = HttpUtil.createPost(WEATHER_URL)
                    .execute()
                    .body();
        } catch (Exception e) {
            log.error("发送请求失败" + e.getMessage());
        }
        log.info("天气信息:{}", responseBody);
    }

    @Scheduled(cron = "0 20 7 * * *")
    public void sendPostJson() {
        String responseBody = null;

        ContentDTO contentDTO = new ContentDTO();
        contentDTO.setContent("TestMessage");

        MessageDTO messageDTO = new MessageDTO();
        messageDTO.setMsgtype("text");
        messageDTO.setText(contentDTO);
        try {
            responseBody = HttpUtil.createPost(ROBOT_URL)
                    .body(JSONUtil.toJsonStr(messageDTO))
                    .execute()
                    .body();
        } catch (Exception e) {
            log.error("发送请求失败" + e.getMessage());
        }

        log.info("返回结果:{}", responseBody);
    }

}
